import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class StudentFile {
    public StudentFile() {
        File file = new File("student.txt");
        if(!file.exists())
        {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void printself(String user)
    {
        ArrayList arrayList = getArrayList();
        for(int i=0;i<arrayList.size();i++)
        {
            Student student = new Student();
            student = (Student) arrayList.get(i);
            if(student.getId().equals(user))
            {
                System.out.println(student.getId()+"\t"+student.getPwd()+"\t"+student.getName()+"\t"+student.getCell()+"\t"+student.getDorm()+"\t"+
                        student.getCla()+"\t"+student.getScore());}
        }
    }
    public void printall()
    {

        ArrayList arrayList = getArrayList();
        for(int i=0;i<arrayList.size();i++)
        {
            Student student = new Student();
            student= (Student) arrayList.get(i);
            System.out.println(student.getId()+"\t"+student.getPwd()+"\t"+student.getName()+"\t"+student.getCell()+"\t"+student.getDorm()+"\t"+
                    student.getCla()+"\t"+student.getScore());

        }
    }
    public void updatePwd(String user){
        Scanner in = new Scanner(System.in);
        ArrayList arrayList = new ArrayList();
        arrayList = getArrayList();
        System.out.println("请输入更新密码");
        String pwd = in.nextLine();
        for(int i=0;i<arrayList.size();i++)
        {
            Student student = (Student)arrayList.get(i);
            if(student.getId().equals(user));
                student.setPwd(pwd);
        }
        SaveStudentFile(arrayList);
    }
    public void SaveStudentFile(ArrayList arrayList){
        try {
            FileWriter fw = new FileWriter("student.txt");
            for(int i=0;i<arrayList.size();i++)
            {
                Student student = new Student();
                student = (Student)arrayList.get(i);
                fw.append(student.getId()+"\t"+student.getPwd()+"\t"+student.getName()+"\t"+student.getCell()+"\t"+student.getDorm()+"\t"+
                        student.getCla()+"\t"+student.getScore());
                fw.append("\n");
            }
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public ArrayList getArrayList(){
        ArrayList arrayList= new ArrayList();
        try {
            FileReader fr = new FileReader("student.txt");
            BufferedReader br = new BufferedReader(fr);
            String buf = null;
            while((buf=br.readLine())!=null)
            {
                Student student = new Student();
                String[] strings= buf.split("\t");
                student.setId(strings[0]);
                student.setPwd(strings[1]);
                student.setName(strings[2]);
                student.setCell(strings[3]);
                student.setDorm(strings[4]);
                student.setCla(strings[5]);
                student.setScore(Integer.parseInt(strings[6]));
                arrayList.add(student);
            }
            br.close();
            fr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return arrayList;
    }
}